package com.dsteiner.widgets.client.controller;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import com.dsteiner.widgets.client.Widgets.HTMLExt;
import com.dsteiner.widgets.client.Widgets.IHTML;
import com.dsteiner.widgets.client.Widgets.ILazyWidget;
import com.dsteiner.widgets.client.Widgets.IWidget;
import com.dsteiner.widgets.client.Widgets.TextInput;
import com.dsteiner.widgets.client.Widgets.WidgetListenerAdapter;
import com.dsteiner.widgets.client.model.PrefilledTextBoxModel;
import com.dsteiner.widgets.client.resources.WidgetsResources;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.TextBox;

public class PrefilledTextBoxController extends TextBoxController{

	private IHTML defaultTextHtml;
	private TextInput prefilledTextBox;
	private PrefilledTextBoxModel prefilledTextBoxModel;
	private IHTML countHtml;

	public void registerCountHtml(IHTML countHtml) {
		this.countHtml = countHtml;
	}

	public void registerDefaultTextHtml(IHTML defaultTextHtml) {
		this.defaultTextHtml = defaultTextHtml;
		defaultTextHtml.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent event) {
				prefilledTextBoxModel.setHasFocus(true);
				event.preventDefault();
			}
		});
	}
	
	public void registerPrefilledTextBox(ILazyWidget prefilledPasswordTextBox) {
		prefilledPasswordTextBox.addWidgetListener(new WidgetListenerAdapter(){
			@Override
			public void onInit(IWidget widget) {
				prefilledTextBox.setText("");
				updateDefaultText();
				updateDefaultTextVisibility();
				updateDefaultTextStyleName();
				updateCountHtml();
			}
		});
	}
	
	@Override
	public void registerPrefilledTextBoxModel(final PrefilledTextBoxModel prefilledTextBoxModel) {
		super.registerPrefilledTextBoxModel(prefilledTextBoxModel);
		this.prefilledTextBoxModel = prefilledTextBoxModel;
		prefilledTextBoxModel.addPropertyChangeListener("defaultText", new PropertyChangeListener(){
			public void propertyChange(PropertyChangeEvent evt) {
				updateDefaultText();
			}
		});
		prefilledTextBoxModel.addPropertyChangeListener("text", new PropertyChangeListener(){
			public void propertyChange(PropertyChangeEvent evt) {
				updateDefaultTextVisibility();
				updateCountHtml();
				checkMaxCount();
			}
		});
		prefilledTextBoxModel.addPropertyChangeListener("maxCount", new PropertyChangeListener(){
			public void propertyChange(PropertyChangeEvent evt) {
				updateCountHtml();
			}
		});
		prefilledTextBoxModel.addPropertyChangeListener("hasFocus", new PropertyChangeListener(){
			public void propertyChange(PropertyChangeEvent evt) {
				updateDefaultTextVisibility();
				updateDefaultTextStyleName();
				updateCountHtml();
			}
		});
	}

	@Override
	public void registerTextBox(TextInput prefilledTextBox) {
		super.registerTextBox(prefilledTextBox);
		this.prefilledTextBox = prefilledTextBox;
	}

	private void checkMaxCount() {
		String text = prefilledTextBoxModel.getText();
		Integer maxCount = prefilledTextBoxModel.getMaxCount();
		if(maxCount != null && text != null && text.length() > maxCount){
			prefilledTextBoxModel.setText(text.substring(0, maxCount));
		}
	}
	
	private int getLeftCount() {
		Integer maxCount = prefilledTextBoxModel.getMaxCount();
		String text = prefilledTextBoxModel.getText();
		int currentCount = text != null ? text.length() : 0;
		int leftCount = maxCount - currentCount;
		return leftCount;
	}

	private void updateCountHtml() {
		Integer maxCount = prefilledTextBoxModel.getMaxCount();
		boolean hasFocus = prefilledTextBoxModel.hasFocus();
		countHtml.setVisible(maxCount != null && hasFocus);
		if(maxCount != null){
			int leftCount = getLeftCount();
			countHtml.setHTML("quedan "+leftCount+" caracteres");
		}
	}

	private void updateDefaultText() {
		defaultTextHtml.setHTML(prefilledTextBoxModel.getDefaultText());
	}

	private void updateDefaultTextStyleName() {
		defaultTextHtml.setStyleName(WidgetsResources.INSTANCE.css().defaultText());
		if(prefilledTextBoxModel.hasFocus())
			defaultTextHtml.addStyleName(WidgetsResources.INSTANCE.css().defaultTextSelected());
	}

	private void updateDefaultTextVisibility() {
		boolean defaultHtmlVisible = !prefilledTextBoxModel.isEdited();
		defaultTextHtml.setVisible(defaultHtmlVisible);
		if(defaultHtmlVisible)
			prefilledTextBoxModel.setText("");
	}
	
}
